{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "28f828bf",
   "metadata": {},
   "source": [
    "# 1.py 使用API模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "66106add",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "094dbf74",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "base_url=\"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "#2.api账户、通行证\n",
    "API_key=\"tYbSZyPTTXYoen3JmH8-W00URCoFSYIh\"\n",
    "API_sercret=\"aQVaoY_WHHFGk1lwJxLpqscDBgk1U_GW\"\n",
    "\n",
    "#3. 请求方法：post，\n",
    "# 4.准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    \n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "# 6. requests\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "e1977f94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "b58d11f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647867138,41888a95-86eb-40a0-a14b-507a37b2b24d',\n",
       " 'time_used': 164,\n",
       " 'faces': [{'face_token': 'dac6c0ae884989db7b102edbc2683deb',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "52d7d19b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'dac6c0ae884989db7b102edbc2683deb'"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()[\"faces\"][0][\"face_token\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "b936f722",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'dac6c0ae884989db7b102edbc2683deb'"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token=r.json()[\"faces\"][0][\"face_token\"]\n",
    "face_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e684b28f",
   "metadata": {},
   "source": [
    "# 2.人脸分析api\n",
    "* 使用模板调用参数实现人脸分析api\n",
    "* 要求：获取return_attributes的所有参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "05008658",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "analyza_url=\"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "#2.api账户、通行证\n",
    "API_key=\"tYbSZyPTTXYoen3JmH8-W00URCoFSYIh\"\n",
    "API_sercret=\"aQVaoY_WHHFGk1lwJxLpqscDBgk1U_GW\"\n",
    "\n",
    "#3. 请求方法：post，\n",
    "# 4.准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':face_token,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus'\n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "# 6. requests\n",
    "r = requests.post(analyza_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "be37b595",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "858f8912",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"time_used\": 172, \"request_id\": \"1647867144,402ee99e-8bde-4699-b1d7-ff587eea4a47\", \"faces\": [{\"attributes\": {\"emotion\": {\"sadness\": 0.681, \"neutral\": 0.007, \"disgust\": 1.213, \"anger\": 0.007, \"surprise\": 0.009, \"fear\": 0.112, \"happiness\": 97.97}, \"beauty\": {\"female_score\": 56.631, \"male_score\": 56.694}, \"gender\": {\"value\": \"Male\"}, \"age\": {\"value\": 53}, \"mouthstatus\": {\"close\": 0.0, \"surgical_mask_or_respirator\": 0.0, \"open\": 100.0, \"other_occlusion\": 0.0}, \"glass\": {\"value\": \"None\"}, \"skinstatus\": {\"dark_circle\": 1.982, \"stain\": 12.624, \"acne\": 0.634, \"health\": 9.525}, \"headpose\": {\"yaw_angle\": 0.004189467, \"pitch_angle\": 16.763775, \"roll_angle\": 2.3050764}, \"blur\": {\"blurness\": {\"threshold\": 50.0, \"value\": 0.364}, \"motionblur\": {\"threshold\": 50.0, \"value\": 0.364}, \"gaussianblur\": {\"threshold\": 50.0, \"value\": 0.364}}, \"smile\": {\"threshold\": 50.0, \"value\": 100.0}, \"eyestatus\": {\"left_eye_status\": {\"normal_glass_eye_open\": 0.002, \"no_glass_eye_close\": 0.0, \"occlusion\": 0.0, \"no_glass_eye_open\": 99.998, \"normal_glass_eye_close\": 0.0, \"dark_glasses\": 0.0}, \"right_eye_status\": {\"normal_glass_eye_open\": 0.031, \"no_glass_eye_close\": 0.0, \"occlusion\": 0.008, \"no_glass_eye_open\": 99.961, \"normal_glass_eye_close\": 0.0, \"dark_glasses\": 0.0}}, \"facequality\": {\"threshold\": 70.1, \"value\": 85.658}, \"eyegaze\": {\"right_eye_gaze\": {\"position_x_coordinate\": 0.468, \"vector_z_component\": 0.944, \"vector_x_component\": -0.054, \"vector_y_component\": 0.325, \"position_y_coordinate\": 0.443}, \"left_eye_gaze\": {\"position_x_coordinate\": 0.506, \"vector_z_component\": 0.948, \"vector_x_component\": 0.168, \"vector_y_component\": 0.269, \"position_y_coordinate\": 0.436}}}, \"face_rectangle\": {\"width\": 107, \"top\": 108, \"left\": 141, \"height\": 107}, \"face_token\": \"dac6c0ae884989db7b102edbc2683deb\"}]}'"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "df8abb98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Response.json of <Response [200]>>"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "3c0af18e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "22e9b4ba",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>dac6c0ae884989db7b102edbc2683deb</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <td>0.681</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <td>0.007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <td>1.213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <td>0.007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <td>0.009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <td>0.112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <td>97.97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>56.631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>56.694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.surgical_mask_or_respirator</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.open</th>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.other_occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.glass.value</th>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.dark_circle</th>\n",
       "      <td>1.982</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.stain</th>\n",
       "      <td>12.624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.acne</th>\n",
       "      <td>0.634</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.health</th>\n",
       "      <td>9.525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.yaw_angle</th>\n",
       "      <td>0.004189</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.pitch_angle</th>\n",
       "      <td>16.763775</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.roll_angle</th>\n",
       "      <td>2.305076</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.value</th>\n",
       "      <td>0.364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.value</th>\n",
       "      <td>0.364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.value</th>\n",
       "      <td>0.364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <td>99.998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.031</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <td>0.008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_open</th>\n",
       "      <td>99.961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.threshold</th>\n",
       "      <td>70.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.value</th>\n",
       "      <td>85.658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.468</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_z_component</th>\n",
       "      <td>0.944</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.054</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_y_component</th>\n",
       "      <td>0.325</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.443</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.506</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_z_component</th>\n",
       "      <td>0.948</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_x_component</th>\n",
       "      <td>0.168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_y_component</th>\n",
       "      <td>0.269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                   0\n",
       "face_token                                          dac6c0ae884989db7b102edbc2683deb\n",
       "attributes.emotion.sadness                                                     0.681\n",
       "attributes.emotion.neutral                                                     0.007\n",
       "attributes.emotion.disgust                                                     1.213\n",
       "attributes.emotion.anger                                                       0.007\n",
       "attributes.emotion.surprise                                                    0.009\n",
       "attributes.emotion.fear                                                        0.112\n",
       "attributes.emotion.happiness                                                   97.97\n",
       "attributes.beauty.female_score                                                56.631\n",
       "attributes.beauty.male_score                                                  56.694\n",
       "attributes.gender.value                                                         Male\n",
       "attributes.age.value                                                              53\n",
       "attributes.mouthstatus.close                                                     0.0\n",
       "attributes.mouthstatus.surgical_mask_or_respirator                               0.0\n",
       "attributes.mouthstatus.open                                                    100.0\n",
       "attributes.mouthstatus.other_occlusion                                           0.0\n",
       "attributes.glass.value                                                          None\n",
       "attributes.skinstatus.dark_circle                                              1.982\n",
       "attributes.skinstatus.stain                                                   12.624\n",
       "attributes.skinstatus.acne                                                     0.634\n",
       "attributes.skinstatus.health                                                   9.525\n",
       "attributes.headpose.yaw_angle                                               0.004189\n",
       "attributes.headpose.pitch_angle                                            16.763775\n",
       "attributes.headpose.roll_angle                                              2.305076\n",
       "attributes.blur.blurness.threshold                                              50.0\n",
       "attributes.blur.blurness.value                                                 0.364\n",
       "attributes.blur.motionblur.threshold                                            50.0\n",
       "attributes.blur.motionblur.value                                               0.364\n",
       "attributes.blur.gaussianblur.threshold                                          50.0\n",
       "attributes.blur.gaussianblur.value                                             0.364\n",
       "attributes.smile.threshold                                                      50.0\n",
       "attributes.smile.value                                                         100.0\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                             0.002\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                               0.0\n",
       "attributes.eyestatus.left_eye_status.occlusion                                   0.0\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                            99.998\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                               0.0\n",
       "attributes.eyestatus.left_eye_status.dark_glasses                                0.0\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                             0.031\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                               0.0\n",
       "attributes.eyestatus.right_eye_status.occlusion                                0.008\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                            99.961\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                               0.0\n",
       "attributes.eyestatus.right_eye_status.dark_glasses                               0.0\n",
       "attributes.facequality.threshold                                                70.1\n",
       "attributes.facequality.value                                                  85.658\n",
       "attributes.eyegaze.right_eye_gaze.position_x_co...                             0.468\n",
       "attributes.eyegaze.right_eye_gaze.vector_z_comp...                             0.944\n",
       "attributes.eyegaze.right_eye_gaze.vector_x_comp...                            -0.054\n",
       "attributes.eyegaze.right_eye_gaze.vector_y_comp...                             0.325\n",
       "attributes.eyegaze.right_eye_gaze.position_y_co...                             0.443\n",
       "attributes.eyegaze.left_eye_gaze.position_x_coo...                             0.506\n",
       "attributes.eyegaze.left_eye_gaze.vector_z_compo...                             0.948\n",
       "attributes.eyegaze.left_eye_gaze.vector_x_compo...                             0.168\n",
       "attributes.eyegaze.left_eye_gaze.vector_y_compo...                             0.269\n",
       "attributes.eyegaze.left_eye_gaze.position_y_coo...                             0.436\n",
       "face_rectangle.width                                                             107\n",
       "face_rectangle.top                                                               108\n",
       "face_rectangle.left                                                              141\n",
       "face_rectangle.height                                                            107"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces']).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdd9276c",
   "metadata": {},
   "source": [
    "# 3. 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5180c9d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "base_url=\"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "#2.api账户、通行证\n",
    "API_key=\"tYbSZyPTTXYoen3JmH8-W00URCoFSYIh\"\n",
    "API_sercret=\"aQVaoY_WHHFGk1lwJxLpqscDBgk1U_GW\"\n",
    "\n",
    "#3. 请求方法：post，\n",
    "# 4.准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    \n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "# 6. requests\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "a3008096",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'d21d957c819b73b7577d3dd29aa397a9'"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01=r.json()[\"faces\"][0][\"face_token\"]\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf2cad70",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "base_url=\"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "#2.api账户、通行证\n",
    "API_key=\"tYbSZyPTTXYoen3JmH8-W00URCoFSYIh\"\n",
    "API_sercret=\"aQVaoY_WHHFGk1lwJxLpqscDBgk1U_GW\"\n",
    "\n",
    "#3. 请求方法：post，\n",
    "# 4.准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    \n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "# 6. requests\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "d6528db0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'d21d957c819b73b7577d3dd29aa397a9'"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02=r.json()[\"faces\"][0][\"face_token\"]\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb93af9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "41893049",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "base_url=\"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "#2.api账户、通行证\n",
    "API_key=\"tYbSZyPTTXYoen3JmH8-W00URCoFSYIh\"\n",
    "API_sercret=\"aQVaoY_WHHFGk1lwJxLpqscDBgk1U_GW\"\n",
    "\n",
    "#3. 请求方法：post，\n",
    "# 4.准备payload（）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "     \"face_token1\":face_token01,  \n",
    "    \"face_token2\":face_token02\n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "# 6. requests\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "897edb71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "4f4a1b5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647866976,12069565-df8f-46a1-a2e1-8e80736e7109',\n",
       " 'time_used': 502,\n",
       " 'confidence': 97.389,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93c656e6",
   "metadata": {},
   "source": [
    "# 4.封装人脸检测函数，实现快速调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "33743fdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key=\"tYbSZyPTTXYoen3JmH8-W00URCoFSYIh\"\n",
    "API_sercret=\"aQVaoY_WHHFGk1lwJxLpqscDBgk1U_GW\"\n",
    "\n",
    "img_url_01 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=1'\n",
    "img_url_02 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "f1c11155",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "cb600258",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647868670,0c87d1c6-d0b9-4378-935e-37f33dc7af8d',\n",
       " 'time_used': 724,\n",
       " 'faces': [{'face_token': 'c84516087364bd4ec806e76bf10b772e',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "c0f5d903",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647868682,c579d132-4bc1-47e7-b682-b4a39beaa008',\n",
       " 'time_used': 183,\n",
       " 'faces': [{'face_token': '5763b4f70114f56124ee445410af2f3a',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acb2275a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
